package com.example.ims.fame;


import com.example.ims.pojo.Commodity;
import com.example.ims.pojo.SalesOrder;
import com.example.ims.service.CommodityService;
import com.example.ims.service.SalesOrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

//@ComponentScan("com.example.ims.service")
public class StockMonitorFame extends JDialog {
    // 用于接收外部传入的主面板，以便设置相对位置等（可根据实际情况调整此逻辑）

    private CommodityService commodityService;
    private SalesOrderService salesOrderService;

    public StockMonitorFame(JPanel parentPanel,ApplicationContext context) {
        salesOrderService=context.getBean(SalesOrderService.class);
        commodityService=context.getBean(CommodityService.class);

        setTitle("入库操作");
        setModal(true);
        setLayout(new BorderLayout());

        // 整体内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(5, 2));

        // 商品名称输入框及标签
        JLabel productNameLabel = new JLabel("商品名称:");
        JTextField productNameTextField = new JTextField();
        contentPanel.add(productNameLabel);
        contentPanel.add(productNameTextField);

        // 商品数量输入框及标签
        JLabel quantityLabel = new JLabel("商品数量:");
        JTextField quantityTextField = new JTextField();
        contentPanel.add(quantityLabel);
        contentPanel.add(quantityTextField);

        // 供应商输入框及标签
        JLabel supplierLabel = new JLabel("供应商:");
        JTextField supplierTextField = new JTextField();
        contentPanel.add(supplierLabel);
        contentPanel.add(supplierTextField);

        JLabel placeLabel = new JLabel("地点:");
        JTextField placeTextField = new JTextField();
        contentPanel.add(placeLabel);
        contentPanel.add(placeTextField);
        // 规格选择框及标签
        JLabel sizeLabel = new JLabel("规格:");
        String[] sizeOptions = {"大", "中", "小"};
        JComboBox<String> sizeComboBox = new JComboBox<>(sizeOptions);
        contentPanel.add(sizeLabel);
        contentPanel.add(sizeComboBox);

        // 确认和取消按钮面板
        JPanel buttonPanel = new JPanel();
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的值
                String productName = productNameTextField.getText();
                String quantityStr = quantityTextField.getText();
                String supplier = supplierTextField.getText();
                String selectedSize = (String) sizeComboBox.getSelectedItem();
                String location=placeTextField.getText();

                try {
                    // 创建Commodity对象并设置属性
                    Commodity commodity = new Commodity();
                    commodity.setName(productName);
                    commodity.setQuantity(Integer.parseInt(quantityStr));
                    commodity.setSupplier(supplier);
                    commodity.setSize(selectedSize);
                    commodity.setLocation(location);
                    System.out.println(commodity.getName());
                    // 调用商品入库业务逻辑方法
                    commodityService.stockIn(commodity);

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date now = new Date();
                    String currentTimeStr = sdf.format(now);
                    SalesOrder salesOrder=new SalesOrder();
                    salesOrder.setOrderType("入库记录");
                    salesOrder.setCompletionTime(currentTimeStr);
                    salesOrder.setSupplier(supplier);
                    salesOrderService.addOrder(salesOrder);


                    // 弹出提示框告知用户入库成功
                    JOptionPane.showMessageDialog(null, "商品入库成功", "入库提示", JOptionPane.INFORMATION_MESSAGE);
                    dispose();  // 关闭对话框
                } catch (NumberFormatException ex) {
                    // 如果数量输入格式不正确，弹出错误提示框
                    JOptionPane.showMessageDialog(null, "商品数量输入格式不正确", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (IllegalArgumentException ex) {
                    // 如果业务逻辑层抛出数量不能为负数等异常，弹出相应错误提示框
                    JOptionPane.showMessageDialog(null, "商品入库失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                } catch (Exception ex) {
                    // 其他未知异常，弹出系统错误提示框
                    JOptionPane.showMessageDialog(null, "系统内部错误，请联系管理员", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();  // 关闭对话框
            }
        });

        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);

        add(contentPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置对话框大小并使其显示在相对合适的位置（相对父面板）
        setSize(300, 200);
        setLocationRelativeTo(parentPanel);
    }


}